package weaver.formmode.customjavacode.customsearch;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.Map;

/**
 * 系数上报的权限控制
 *
 * @author Administrator
 */
public class CustomSearchXSSBQXKZ extends AbstractCustomSqlConditionJavaCode {

    /**
     * 生成SQL查询限制条件
     *
     * @param param param包含(但不限于)以下数据
     *              user 当前用户
     * @return 返回的查询限制条件的格式举例为: t1.a = '1' and t1.b = '3' and t1.c like '%22%'
     * 其中t1为表单主表表名的别名
     */

    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet recordSet = new RecordSet();

        User user = (User) param.get("user");
        int userid = user.getUID();
        baseBean.writeLog("generateSqlCondition——userid:" + userid);

        String sqlCondition = " (1=0 ";

        //数据管理员表
        String SJGLYtablename = baseBean.getPropValue("Mode_Qxkz", "SJGLYtablename");
        //数据上报员表
        String SJSBYtablename = baseBean.getPropValue("Mode_Qxkz", "SJSBYtablename");
        //系数上报获取日志表
        String RZtablename = baseBean.getPropValue("Mode_Qxkz", "XSSBHQRZBtablename");
//        //信息员视图
//        String XSYtablename = baseBean.getPropValue("Mode_Qxkz", "XSYtablename");
//        //区域人事专员视图
//        String QYRSZYtablename = baseBean.getPropValue("Mode_Qxkz", "QYRSZYtablename");
//        //机构类型：16的部门负责人视图
//        String BMFZR16tablename = baseBean.getPropValue("Mode_Qxkz", "BMFZR16tablename");
//        //机构类型：32的部门负责人视图
//        String BMFZR32tablename = baseBean.getPropValue("Mode_Qxkz", "BMFZR32tablename");
//        //机构类型：34的部门负责人视图
//        String BMFZR34tablename = baseBean.getPropValue("Mode_Qxkz", "BMFZR34tablename");
//        //机构类型：36的部门负责人视图
//        String BMFZR36tablename = baseBean.getPropValue("Mode_Qxkz", "BMFZR36tablename");
        //系数上报新增权限表
        String STtablename = baseBean.getPropValue("Mode_Qxkz", "XSSBSTtablename");

        boolean flag = false;
        rs.executeQuery("select ry from " + SJGLYtablename);
        while (rs.next()) {
            if (user.getUID() == Util.getIntValue(rs.getString("ry"))) {
                flag = true;
                break;
            }
        }

        if (user.getUID() == 1 || flag) {
            //系统管理员和数据管理员能看到全部数据
            sqlCondition = " 1=1 ";
            baseBean.writeLog("CustomSearchXSSBQXKZ——sqlCondition:" + sqlCondition);
            return sqlCondition;
        } else {
            //遍历  数据上报员表
            rs.execute("select * from " + SJSBYtablename);
            while (rs.next()) {
                if (user.getUID() == Util.getIntValue(rs.getString("ry"))) {
                    recordSet.execute("select * from " + RZtablename + " where ry='" + user.getUID() + "'");
                    while (recordSet.next()) {
                        String GJAHR = Util.null2String(recordSet.getString("GJAHR"));
                        String MONAT = Util.null2String(recordSet.getString("MONAT"));
                        String WERKS = Util.null2String(recordSet.getString("WERKS"));
                        sqlCondition += " or (t1.GJAHR='" + GJAHR + "' and t1.MONAT='" + MONAT + "' and t1.WERKS='" + WERKS +
                                "') ";
                    }
                    sqlCondition += ") ";
                    baseBean.writeLog("CustomSearchXSSBQXKZ——sqlCondition:" + sqlCondition);
                    return sqlCondition;
                }
            }

            String[] st = STtablename.split(",");
            for (String tablename : st) {
                //遍历  视图
                rs.execute("select * from " + tablename);
                while (rs.next()) {
                    if (user.getUID() == Util.getIntValue(rs.getString("id"))) {
                        recordSet.execute("select * from " + RZtablename + " where ry='" + user.getUID() + "'");
                        while (recordSet.next()) {
                            String GJAHR = Util.null2String(recordSet.getString("GJAHR"));
                            String MONAT = Util.null2String(recordSet.getString("MONAT"));
                            String WERKS = Util.null2String(recordSet.getString("WERKS"));
                            sqlCondition += " or (t1.GJAHR='" + GJAHR + "' and t1.MONAT='" + MONAT + "' and " +
                                    "t1.WERKS='" + WERKS + "') ";
                        }
                        sqlCondition += ") ";
                        baseBean.writeLog("CustomSearchXSSBQXKZ——sqlCondition:" + sqlCondition);
                        return sqlCondition;
                    }
                }
            }

        }

        sqlCondition += ") ";
        baseBean.writeLog("CustomSearchXSSBQXKZ——sqlCondition:" + sqlCondition);
        return sqlCondition;
    }

}
